package com.netease.xone.itemview;

import java.util.LinkedList;

import protocol.XoneCallBack;
import protocol.XoneService;
import protocol.meta.AppInfo;
import protocol.meta.AppInfoDetail;
import Utils.StackTraceUtil;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.netease.log.NTLog;
import com.netease.xone.R;
import com.netease.xone.app.XoneApp;


public class RenderInfoFoot implements IRenderInfoBase, OnClickListener{
    public static final String TAG = RenderInfoFoot.class.getSimpleName();
    
    private View mRoot;
    private TextView mCommentCount;
    private TextView mForwardCount;
    private TextView mPraiseCount;
    private LinearLayout mPraise;

    private boolean mPraiseFlag;
    private String mInfoId;
    private LinkedList<Integer> mReqList = new LinkedList<Integer>();
    
    public RenderInfoFoot(View v) {
        mRoot = v;
        mCommentCount = (TextView)v.findViewById(R.id.info_comment_count);
        mForwardCount = (TextView)v.findViewById(R.id.info_forward_count);
        mPraiseCount = (TextView)v.findViewById(R.id.info_praise_count);
        mPraise = (LinearLayout)v.findViewById(R.id.info_praise);
        
        mPraise.setOnClickListener(this);
    }

    @Override
    public void renderView(Context context, AppInfo appInfo) {
        
        renderView(context, 
                appInfo.getInfoId(),
                appInfo.getCommentCount() == null ? 0 : appInfo.getCommentCount(), 
                appInfo.getForwardCount() == null ? 0 : appInfo.getForwardCount(),
                appInfo.getPraiseCount() == null ? 0 : appInfo.getPraiseCount(),
                appInfo.getPraiseFlag() == null ? false : appInfo.getPraiseFlag());
    }

    @Override
    public void renderView(Context context, AppInfoDetail appInfoDetail) {
        renderView(context, 
                appInfoDetail.getInfoId(),
                appInfoDetail.getCommentCount() == null ? 0 : appInfoDetail.getCommentCount(), 
                appInfoDetail.getForwardCount() == null ? 0 : appInfoDetail.getForwardCount(),
                appInfoDetail.getPraiseCount() == null ? 0 : appInfoDetail.getPraiseCount(),
                appInfoDetail.getPraiseFlag() == null ? false : appInfoDetail.getPraiseFlag());
    }
    
    private void renderView(Context context, String infoId, int commentCount, int forwardCount, int praiseCount, boolean praiseFlag) {
        NTLog.d(TAG, StackTraceUtil.getMethodName() 
                + " infoId=" + infoId
                + " getCommentCount()=" + commentCount
                + " getForwardCount()=" + forwardCount
                + " getPraiseCount()=" + praiseCount
                + " getPraiseFlag()=" + praiseFlag
                );
        mCommentCount.setText(String.valueOf(commentCount));
        mForwardCount.setText(String.valueOf(forwardCount));
        mPraiseCount.setText(String.valueOf(praiseCount));
        
        mInfoId = infoId;
        mPraiseFlag = praiseFlag;
    }
    
    @Override
    public void onClick(View v) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        if (XoneApp.getAppInstance().isLoginUser(mRoot.getContext())) {
            XoneService.getInstance().addListener(mXoneCallBack);
            int tid = XoneService.getInstance().doPraiseInfo(mInfoId, !mPraiseFlag);
            mReqList.add(tid);
        }
    }
    
    private XoneCallBack mXoneCallBack = new XoneCallBack() {
        @Override
        public void onPraiseInfo(int transactionId, Object obj) {
            if (mReqList.remove(Integer.valueOf(transactionId))) {
                XoneService.getInstance().removeListener(mXoneCallBack);
            } 
        };
        
        @Override
        public void onPraiseInfoError(int transactionId, int errCode, String err) {
            if (mReqList.remove(Integer.valueOf(transactionId))) {
                XoneService.getInstance().removeListener(mXoneCallBack);
            } 
        };
    };
}
